package com.mz.ozy.activity.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.LoginBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.CodePresenter;
import com.mz.ozy.mvp.presenter.LoginPresenter;
import com.mz.ozy.mvp.presenter.SetPassPresenter;
import com.mz.ozy.mvp.view.CodeContract;
import com.mz.ozy.mvp.view.LoginContract;
import com.mz.ozy.mvp.view.SetPassContract;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.SharePreferencesData;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetPasswordActivity extends BaseActivity<SetPassContract.View, SetPassPresenter> implements SetPassContract.View {

    @BindView(R.id.et_invite_code)
    EditText mEtInviteCode;
    @BindView(R.id.etPass)
    EditText mEtPass;
    @BindView(R.id.etRePassword)
    EditText mEtRePassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_password);
        ButterKnife.bind(this);
        initTitle();
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        AppUtils.resetStaticData(this);
        super.onBackPressed();
    }

    @OnClick({R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_next:
                setPassword();
                break;
        }
    }



    private void setPassword() {
        String mCode = mEtInviteCode.getText().toString().trim();
        if (TextUtils.isEmpty(mCode)) {
            //TODO 默认推荐码为0 不做参考意见
            mCode = "0";
//            ToastUtils.showToast("请输入邀请码!");
//            return;
        }
        String pass = mEtPass.getText().toString().trim();
        if (TextUtils.isEmpty(pass) || pass.length() < 6 || !Utils.ispsd(pass)) {
            ToastUtils.showToast("请输入6-20位数字和字母组合");
            return;
        }
        String repass = mEtRePassword.getText().toString().trim();
        if (TextUtils.isEmpty(repass)) {
            ToastUtils.showToast("请再次输入密码");
            return;
        }
        if (!pass.equals(repass)) {
            ToastUtils.showToast(getString(R.string.str_loginpwd_inconsistent));
            return;
        }
        mPresenter.setPassword(this, mCode, pass, repass);
    }

    @Override
    public void setPassSuccess(LoginBean loginBean) {
        SPUtils.getInstance(this).putString(Constants.PREE.IS_PAY_PWD, "1");
        Constants.isPayPwd = true;
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
